package com.leyou.item.web;


import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     *
     * @param page 当前页
     * @param rows 每页大小
     * @param key  搜索关键字
     * @param sortBy 排序字段
     * @param desc 是否降序
     * @return 当前页结果
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc
    ){
        return ResponseEntity.ok(brandService.queryResult(page,rows,key,sortBy,desc));
    }

    /**
     *    添加品牌
     * @param brand
     * @param ids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brand, @RequestParam("cids")List<Long> ids){
        brandService.saveBrand(brand,ids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping
    public ResponseEntity<Void> deleteBrand(@RequestParam("id") Long id){
        brandService.deleteBrand(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO,@RequestParam("cids")List<Long> ids){
        brandService.updateBrand(brandDTO,ids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类查询分类相关的品牌
     * @param cid 商品分类id
     * @return 品牌的集合
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByBrandId(@RequestParam("id") Long cid){

        return ResponseEntity.ok(this.brandService.queryByBrandId(cid));
    }

    /**
     * 根据id查询品牌
     * @param id 品牌的id
     * @return 品牌对象
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id")Long id){

        return ResponseEntity.ok(brandService.queryById(id));
    }

    /**
     * 根据id的集合查询品牌
     * @param idList 品牌id的集合
     * @return 品牌的集合
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList){

        return ResponseEntity.ok(brandService.queryByIds(idList));
    }
}
